<?php

namespace App\Http\Controllers\Finance;

use Illuminate\Http\Request;
use App\Models\Finance\Original;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

/**
 * 薪资原始字段业务逻辑控制器
 *
 * Date: 2018/6/5
 * @author George
 * @package App\Http\Controllers\Finance
 */
class OriginalController extends Controller
{
	/**
	 * 获取公司的原始字段信息
	 *
	 * Date: 2018/6/5
	 * @author George
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function index()
	{
		$originals = Original::all();
		return success($originals);
	}

	/**
	 * 更新或插入原始字段设置
	 *
	 * Date: 2018/6/5
	 * @author George
	 * @deprecated
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function update(Request $request)
	{
		$attributes = $this->validate($request, [
			'original_id' => 'required|integer',
			'status' => 'required|boolean',
		]);

		$user = Auth::user();

		DB::table('company_original_pivot')->updateOrInsert([
			'company_id' => $user->company_id,
			'original_id' => $attributes['original_id']
		], ['status' => $attributes['status']]);

		return updated(array_merge($attributes, ['company_id' => $user->company_id]));
	}
}
